package net.gz01.wallet.web.controller.org;


import java.io.File;
import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.List;

import org.jxls.common.Context;

import com.jfinal.core.Controller;

import lombok.extern.slf4j.Slf4j;
import net.gz01.util.ZOExcel;
import net.gz01.wallet.web.config.SystemEnum.FilePath;
import net.gz01.wallet.web.model.OrgUser;
import net.gz01.wallet.web.model.TradeOrder;

@Slf4j
public class ReportController extends Controller{

	public void index(){
		render("trade/reportList.html");
	}

	public void chargepayReport(){
		OrgUser orgUser = (OrgUser) getSession().getAttribute("loginOrgUser");
		String beginDate = getPara("beginDate");
		String endDate = getPara("endDate");
		List<TradeOrder> orderList = TradeOrder.dao.searchChargepayOrgReport(orgUser.getOrgCode(), beginDate, endDate);
		for (TradeOrder order : orderList) {
			BigDecimal amount = BigDecimal.valueOf(order.getAmount()).divide(BigDecimal.valueOf(100));
			BigDecimal fee = BigDecimal.valueOf(order.getFee()).divide(BigDecimal.valueOf(100));
			BigDecimal balance = BigDecimal.valueOf(order.getBalance()).divide(BigDecimal.valueOf(100));
			if("df_personal".equals(order.getBizType())) {
				order.put("amountY", amount.divide(new BigDecimal(-1)));
				order.setBizType("对私代付");
			} else if("df_company".equals(order.getBizType())) {
				order.put("amountY", amount.divide(new BigDecimal(-1)));
				order.setBizType("对公代付");
			} else{
				order.put("amountY", amount);
				order.setBizType("充值");
			}
			order.put("feeY",fee.divide(new BigDecimal(-1)));
			order.put("balanceY", balance);
		}

		String fileName = "充值代付明细_" + beginDate + "至" + endDate;
		Context context = new Context();
		context.putVar("fileName", fileName);
		context.putVar("list", orderList);
		String templatePath = FilePath.ReportTemplate.getValue() + "chargepay.xlsx";
		String targetPath = FilePath.ReportTmp.getValue() + fileName + ".xlsx";
		LinkedHashMap<String, Context> sheetNameAndData = new LinkedHashMap<>();
		sheetNameAndData.put("充值代付明细", context);
		String[] templateSheetName = { "sheet1" };

		try {
			ZOExcel.exportLargeData(templatePath, targetPath, sheetNameAndData, templateSheetName);
		} catch (Exception e) {
			log.info("导出渠道交易明细报表发生异常");
			e.printStackTrace();
		}
		renderFile(new File(targetPath));
	}

}
